import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _omit from "lodash/omit";
var _excluded = ["onChange", "defaultValue", "rules", "name"];
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Form, Button, Row, Col } from 'antd';
import intl from "../../utils/intl";
import styles from "./index.less?modules";
import map from "./map";
var FormItem = Form.Item;

function generator(_ref) {
  var defaultProps = _ref.defaultProps,
      defaultRules = _ref.defaultRules,
      type = _ref.type;
  return function (WrappedComponent) {
    var _class;

    return _class = /*#__PURE__*/function (_Component) {
      _inherits(BasicComponent, _Component);

      var _super = _createSuper(BasicComponent);

      function BasicComponent(props) {
        var _this;

        _classCallCheck(this, BasicComponent);

        _this = _super.call(this, props);

        _this.onGetCaptcha = function () {
          var count = 59;

          _this.setState({
            count: count
          });

          if (_this.props.onGetCaptcha) {
            _this.props.onGetCaptcha();
          }

          _this.interval = setInterval(function () {
            count -= 1;

            _this.setState({
              count: count
            });

            if (count === 0) {
              clearInterval(_this.interval);
            }
          }, 1000);
        };

        _this.state = {
          count: 0
        };
        return _this;
      }

      _createClass(BasicComponent, [{
        key: "componentDidMount",
        value: function componentDidMount() {
          if (this.context.updateActive) {
            this.context.updateActive(this.props.name);
          }
        }
      }, {
        key: "componentWillUnmount",
        value: function componentWillUnmount() {
          clearInterval(this.interval);
        }
      }, {
        key: "render",
        value: function render() {
          var getFieldDecorator = this.context.form.getFieldDecorator;
          var options = {};
          var otherProps = {};

          var _this$props = this.props,
              onChange = _this$props.onChange,
              defaultValue = _this$props.defaultValue,
              rules = _this$props.rules,
              name = _this$props.name,
              restProps = _objectWithoutProperties(_this$props, _excluded);

          var count = this.state.count;
          options.rules = rules || defaultRules;

          if (onChange) {
            options.onChange = onChange;
          }

          if (defaultValue) {
            options.initialValue = defaultValue;
          }

          otherProps = restProps || otherProps;

          if (type === 'Captcha') {
            var inputProps = _omit(otherProps, ['onGetCaptcha']);

            return /*#__PURE__*/React.createElement(FormItem, null, /*#__PURE__*/React.createElement(Row, {
              gutter: 8
            }, /*#__PURE__*/React.createElement(Col, {
              span: 16
            }, getFieldDecorator(name, options)( /*#__PURE__*/React.createElement(WrappedComponent, Object.assign({}, defaultProps, inputProps)))), /*#__PURE__*/React.createElement(Col, {
              span: 8
            }, /*#__PURE__*/React.createElement(Button, {
              disabled: count,
              className: styles.getCaptcha,
              size: "large",
              onClick: this.onGetCaptcha
            }, count ? intl.get('cpaas.common.components.login.count', {
              count: count
            }).d("".concat(count, " s")) : intl.get('cpaas.common.components.login.captcha').d('获取验证码')))));
          }

          return /*#__PURE__*/React.createElement(FormItem, null, getFieldDecorator(name, options)( /*#__PURE__*/React.createElement(WrappedComponent, Object.assign({}, defaultProps, otherProps))));
        }
      }]);

      return BasicComponent;
    }(Component), _class.contextTypes = {
      form: PropTypes.object,
      updateActive: PropTypes.func
    }, _class;
  };
}

var LoginItem = {};
Object.keys(map).forEach(function (item) {
  LoginItem[item] = generator({
    defaultProps: map[item].props,
    defaultRules: map[item].rules,
    type: item
  })(map[item].component);
});
export default LoginItem;